﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FinalOnlineLibrary.Models;

namespace FinalOnlineLibrary.HtmlHelpers
{
    public static class PagingHelpers
    {
        /// <summary>
        /// Display paging
        /// </summary>
        /// <param name="helper">Extend methods HtmlHelper</param>
        /// <param name="pagingInfo">Instance of PagingInfo class</param>
        /// <param name="pageUrl"></param>
        /// <returns></returns>
        public static MvcHtmlString PageLinks(this HtmlHelper helper,
            PagingInfo pagingInfo,
            Func<int, string> pageUrl) 
        {
            StringBuilder result = new StringBuilder();
            for (int i = 1; i <= pagingInfo.TotalPages; i++)
            {
                TagBuilder tag = new TagBuilder("a");
                tag.MergeAttribute("href", pageUrl(i));
                tag.InnerHtml = i.ToString();
                if (i == pagingInfo.CurrentPage)
                {
                    tag.AddCssClass("pagination-flickr");
                }
                result.AppendLine(tag.ToString());
            }
            return MvcHtmlString.Create(result.ToString());
        }
    }
}